<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

class BannerImageController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //检测横幅设置是否存在
        $image = DB::table('banner_image')
            ->select('id', 'image', 'url', 'set_time', 'set_people')
            ->where(function ($query) {
                $query->orWhere('image', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('url', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('set_time', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('set_people', 'like', '%' . @$_GET['search'] . '%');
            })
            ->paginate(17)
            ->withPath('?search=' . @$_GET['search'] . '');

        return view('admin.banner.image.index',['image'=>$image]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //查询横幅图片信息
        $banner_image_info = DB::table('banner_image')
            ->select('id', 'image', 'url', 'set_time', 'set_people')
            ->where('id',$id)
            ->first();

        return view('admin.banner.image.edit',['banner_image_info' => $banner_image_info]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'image' => ['required','string','max:50'],
            'url' => ['required','string','max:50'],
        ]);

        //更新购买成功到数据库
        DB::table('banner_image')
            ->where('id',$id)
            ->update([
                'image' => $request->image,
                'url' => $request->url
            ]);

        return response()->json(['message' => '修改成功。', 'success' => 'true']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('banner_image')
            ->whereIn('id', explode(',', $id))
            ->delete();

        return response()->json(['message' => '成功删除。', 'success' => 'true']);
    }

}
